package main

import (
	"log"
	"time"
	_ "user-go/controller"

	"github.com/androidsr/sc-go/proxy"
	"github.com/androidsr/sc-go/sbuilder"

	_ "github.com/go-sql-driver/mysql"

	"github.com/androidsr/sc-go/sc"
	"github.com/androidsr/sc-go/sgin"
	"github.com/androidsr/sc-go/sno"
	"github.com/androidsr/sc-go/sorm"
	"github.com/androidsr/sc-go/syaml"
	"github.com/gin-gonic/gin"
)

func main() {
	log.SetFlags(log.Llongfile | log.LstdFlags)
	config, err := syaml.LoadFile[syaml.ScRoot]("sc-go.yaml")
	if err != nil {
		panic(err)
	}

	sorm.DB = sorm.New(config.Sc.Sqlx)
	sno.New(*config.Sc.Snowflake)
	router := sgin.New(config.Sc.Gin)
	router.Cors()
	router.WebToken(config.Sc.Jwt)

	addFill(router)
	go func() {
		router.RunServer()
	}()
	proxy.New(config.Sc.Proxy)
}

func addFill(router *sgin.SGin) {
	sbuilder.AddInsertFill("crt_opr", func() any {
		c := router.GetContent().Get().(*gin.Context)
		return c.GetString("userId")
	})
	sbuilder.AddInsertFill("crt_ts", func() any {
		return sc.FormatDateTimeString(time.Now())
	})
	sbuilder.AddInsertFill("id", func() any {
		return sno.GetString()
	})
}
